<?php

namespace App\Admin\Controllers;

use App\Models\SwitchModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;

class SwitchController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('开关列表')
            ->description('网关')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(SwitchModel::where('pid','0'), function (Grid $grid) {
            $grid->column('name', '名称')->display(function () {
                $url = admin_url('switch-children/'.$this->id);
                    return '<a href="'.$url.'" target="_blank">'.$this->name.'</a>';
            });
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $grid->column('project_id', '所属项目')->using($res);
            $grid->column('machine_code','设备码');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建开关')
            ->description('网关创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new SwitchModel(), function (Form $form) {
            $form->text('name', '名称')->required();
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId');
            $form->select('project_id', '所属项目')->options($res)->required();
            $form->text('machine_code', '设备码')->required();
            $form->text('address', '地址')->default('');

            $form->saving(function (Form $form) {
                $checkRes = SwitchModel::checkMachineCode($form->machine_code);
                if (!$checkRes) {
                    return $form->response()->error('无效的设备码');
                }
            });

            $form->deleted(function (Form $form, $result) {
                if ($result) {
                    $data = $form->model()->toArray();
                    SwitchModel::afterDeleted($data[0]['id']);
                }
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('开关编辑')
            ->description('网关编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    public function show($id){
        return Show::make($id, new SwitchModel(), function (Show $show) {
            $show->field('name','名称');
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $show->field('project_id','所属项目')->using($res);
            $show->field('machine_code','机器码');
            $show->field('address','地址');
        });
    }
}